import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:lianwenke20220712/bean/login_bean.dart';
import 'package:lianwenke20220712/login.dart';
import 'package:shared_preferences/shared_preferences.dart';

/// FileName 连文科
///
/// @Author/// @Date 2022/7/12 16:44
///
/// @Description 我的
class MyWidget extends StatefulWidget {
  const MyWidget({Key key}) : super(key: key);

  @override
  _MyWidgetState createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {
  bool flag;
  String pic,name;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    SpUtils();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          if (flag == true) Container(
            height: 200,
            child: Row(
              children: [
                Container(
                  margin: EdgeInsets.only(left: 20),
                  child: CircleAvatar(
                    radius: 40,
                    backgroundImage: NetworkImage(pic),
                  ),
                ),
                 Container(
                     margin: EdgeInsets.only(left: 20),
                     child: Text(name,style: TextStyle(fontSize: 18),)),
              ],
            ),
          ) else Container(
            height: 250,
            child: Row(
              children: [
                Container(
                  height: 200,
                  margin: EdgeInsets.only(top: 60),
                  child: Column(
                    children: [
                      Text('欢迎来到皮皮虾',style: TextStyle(fontSize: 25),),
                      InkWell(
                        onTap: (){
                          Navigator.of(context).push(MaterialPageRoute(builder: (context){
                            return LoginWidget();
                          })).then((data){
                            SpUtils();
                            var bean = LoginBean.fromJson(data);
                            setState(() {
                              pic = bean.result.headPic;
                              name = bean.result.nickName;
                            });
                          });
                        },
                        child: Container(height: 50,width: 200,margin: EdgeInsets.only(top: 20),
                        color: Colors.redAccent,child: Center(child: Text('登录/注册',style: TextStyle(color: Colors.white),)),),
                      ),
                    ],
                  ),
                ),
                Expanded(child: Image.asset('assets/images/touxiang.png',height: MediaQuery.of(context).size.height,))
              ],
            ),
          ),
          Container(
            height: 50,
            child: Image.asset('assets/images/xiaoguo.png',fit: BoxFit.fill,width: MediaQuery.of(context).size.width,),
          ),
          Container(
            height: 150,
            margin: EdgeInsets.symmetric(horizontal: 10),
            child: Column(
              children: [
                Container(
                  margin: EdgeInsets.only(top: 10),
                  child: Row(
                    children: [
                      Text('历史记录',style: TextStyle(fontSize: 16),),
                      Container(
                          margin: EdgeInsets.only(left: 350),
                          child: Text('>')),
                    ],
                  ),
                ),
                Container(
                  margin: EdgeInsets.only(top: 10),
                  child: Row(
                    children: [
                      Text('创作中心',style: TextStyle(fontSize: 16),),
                      Container(
                          margin: EdgeInsets.only(left: 350),
                          child: Text('>')),
                    ],
                  ),
                ),
                Container(
                  margin: EdgeInsets.only(top: 10),
                  child: Row(
                    children: [
                      Text('原创特权',style: TextStyle(fontSize: 16),),
                      Container(
                          margin: EdgeInsets.only(left: 350),
                          child: Text('>')),
                    ],
                  ),
                ),
              ],
            ),
          ),
          Expanded(child: Container(
            margin: EdgeInsets.only(top: 10),
            padding: EdgeInsets.symmetric(vertical: 10),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text('更多功能',style: TextStyle(fontSize: 16),),
                Container(
                  margin: EdgeInsets.only(top: 15),
                  child: Row(
                    children: [
                      Expanded(
                        child: Column(
                          children: [
                            Image.asset('assets/images/qb.png',width: 35,height: 35,),
                            Text('我的钱包'),
                          ],
                        ),
                      ),
                      Expanded(
                        child: Column(
                          children: [
                            Image.asset('assets/images/qb.png',width: 35,height: 35,),
                            Text('我的钱包'),
                          ],
                        ),
                      ),
                      Expanded(
                        child: Column(
                          children: [
                            Image.asset('assets/images/qb.png',width: 35,height: 35,),
                            Text('我的钱包'),
                          ],
                        ),
                      ),
                      Expanded(
                        child: Column(
                          children: [
                            Image.asset('assets/images/qb.png',width: 35,height: 35,),
                            Text('我的钱包'),
                          ],
                        ),
                      ),
                    ],
                  ),
                ),
                Container(
                  margin: EdgeInsets.only(top: 15),
                  child: Row(
                    children: [
                      Expanded(
                        child: Column(
                          children: [
                            Image.asset('assets/images/qb.png',width: 35,height: 35,),
                            Text('我的钱包'),
                          ],
                        ),
                      ),
                      Expanded(
                        child: Column(
                          children: [
                            Image.asset('assets/images/qb.png',width: 35,height: 35,),
                            Text('我的钱包'),
                          ],
                        ),
                      ),
                      Expanded(
                        child: Column(
                          children: [
                            Image.asset('assets/images/qb.png',width: 35,height: 35,),
                            Text('我的钱包'),
                          ],
                        ),
                      ),
                      Expanded(
                        child: Column(
                          children: [
                            Image.asset('assets/images/qb.png',width: 35,height: 35,),
                            Text('我的钱包'),
                          ],
                        ),
                      ),
                    ],
                  ),
                ),
                Container(
                  margin: EdgeInsets.only(top: 15),
                  child: Row(
                    children: [
                      Expanded(
                        child: Column(
                          children: [
                            Image.asset('assets/images/qb.png',width: 35,height: 35,),
                            Text('我的钱包'),
                          ],
                        ),
                      ),
                      Expanded(
                        child: Column(
                          children: [
                            Image.asset('assets/images/qb.png',width: 35,height: 35,),
                            Text('我的钱包'),
                          ],
                        ),
                      ),
                      Expanded(
                        child: Column(
                          children: [
                            Image.asset('assets/images/qb.png',width: 35,height: 35,),
                            Text('我的钱包'),
                          ],
                        ),
                      ),
                      Expanded(
                        child: Column(
                          children: [
                            Image.asset('assets/images/qb.png',width: 35,height: 35,),
                            Text('我的钱包'),
                          ],
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ))

        ],
      ),
    );
  }

  void SpUtils() async {
    var sp = await SharedPreferences.getInstance();
    flag = sp.getBool('flag');
    var s = sp.getString('userName');
    var decode = jsonDecode(s);
    var bean = LoginBean.fromJson(decode);
    setState(() {
      pic = bean.result.headPic;
      name = bean.result.nickName;
    });
  }
}
